Flutter TransitionRoute completed

In the Flutter framework, the completed property of the TransitionRoute class is crucial for managing the lifecycle and transitions of routes in a navigational context.

The source code of completed is as below:

Future<T?> get completed => _transitionCompleter.future;
final Completer<T?> _transitionCompleter = Completer<T?>();

Here's a detailed explanation:

  1. Purpose of completed Property: The completed property is a Future that signals the completion of a route's transition. It serves as a notifier for when the route has fully finished its transition and is no longer visible in the app's navigation stack.

  2. Transition and Overlay Relationship: The comment mentions that this Future completes "once the transition itself has finished, after the overlay entries have been removed from the navigator's overlay." This implies that the completed property becomes resolved (i.e., the future completes) when the route's visual transition (like a page slide or fade) is fully completed and its visual representation (overlay entries) is removed from the navigator's overlay. The overlay is a stack of visual elements that constitute the route's UI.

  3. Timing Relative to popped Event: The comment also states that this completion occurs "after popped". The [popped] refers to another event in the route's lifecycle, which triggers as soon as the route starts to be removed (or popped) from the navigation stack. The completed future finishes after the popped event, typically once the associated animation ends. This indicates that the route begins its transition out of view when it's popped, but the completed property only resolves after this transition (including any animations) is fully finished.

  4. Role of Completer: The completed property is backed by a Completer object (_transitionCompleter). A Completer in Dart is a way to produce future results asynchronously. The route's logic will trigger the completion of this Completer when the transition is fully done, resolving the completed future.

  5. Use Cases: Developers might use the completed property to perform actions after a route has fully transitioned out and is no longer visible. For example, releasing resources, updating states, or triggering other navigational actions that should only occur after a route is entirely finished.

Can I .then this Future multiple times?

Yes, you can attach multiple .then callbacks to the same Future in Dart, including the completed Future of a TransitionRoute in Flutter. Each .then call registers a callback that will be executed when the Future completes.

Here's what you need to know about using .then multiple times on a Future:

  1. Multiple Callbacks: When you use .then multiple times on the same Future, each callback is registered independently. Once the Future completes, all registered callbacks are invoked in the order they were added.

  2. Independent Execution: Each callback is executed independently of the others. The result of one callback does not affect the others unless you explicitly design them to be interdependent.

  3. Error Handling: If the Future completes with an error, each .then callback that has an error handling mechanism (the second argument of .then) will receive that error. If no error handling is provided, the error will propagate to the next .then callback, or result in an unhandled exception if no error handling is present.

  4. Chaining Futures: You can also chain futures using .then. The callback provided to .then can return a value or another Future. If a new Future is returned, subsequent .then callbacks wait for this new Future to complete.

  5. Use in Flutter and TransitionRoutes: In the context of Flutter's TransitionRoute, using .then multiple times on the completed Future can be useful for triggering multiple actions once the route's transition is finished, such as cleanup, data updates, or initiating other navigational changes.

Remember, while it's technically possible and sometimes useful to attach multiple callbacks to a single Future, it's important to ensure that these callbacks do not conflict with each other, especially in the context of UI updates in Flutter.

If the future is already finished, then a "then" it, It will also work or not?

Yes, if you attach a .then callback to a Future that has already completed, it will still work. In Dart (and Flutter, by extension), when you add a .then callback to a Future that has already finished, the callback will be executed immediately with the value or error that the Future was completed with.

Here's how this behavior is beneficial:

  1. Immediate Invocation: If the Future is already completed, either successfully or with an error, any new .then callbacks are invoked as soon as they are registered. This is due to the asynchronous nature of Dart's event loop.

  2. Handling Late Subscribers: This feature is particularly useful for handling scenarios where you might be dealing with late subscribers to a result that's already available. For instance, in a Flutter app, you might have a Future that completes with data from an API call, and multiple widgets might need to use this data at different times.

  3. Consistent Programming Model: This behavior ensures a consistent programming model where you don't have to worry about the timing of when the .then is attached relative to when the Future completes.

  4. Error Handling: If the Future completed with an error and you attach a .then with an error handler, the error handler will be invoked immediately.

In summary, attaching a .then to an already completed Future in Dart is a safe and expected practice, and the callback will be executed with the resolved value or error of the Future. This makes Futures in Dart a robust tool for asynchronous programming, even in scenarios where the timing of Future completion and callback registration may not be strictly sequential.


本文作者:Maeiee

本文链接:Flutter TransitionRoute completed

版权声明:如无特别声明,本文即为原创文章,版权归 Maeiee 所有,未经允许不得转载!


喜欢我文章的朋友请随缘打赏,鼓励我创作更多更好的作品!